<?php
namespace app\api\controller\service;

use app\api\model\service\GiveMoneyLog;
use app\api\model\service\SearchLog;
use app\common\controller\Api;
use app\common\library\Sms;
use think\Db;
use think\Validate;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['getConfig','getText','trainList','trainDetail','help','templateConfig','extend'];
    protected $noNeedRight = ['*'];

    /**
     * 获取资料配置
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getConfig()
    {
        $row = model('app\api\model\service\ProjectConfig')
            ->where(['state'=>1])
            ->field('service_mobile,cooperate_mobile,user_image,user_name,refund_notice,user_intro,skill_image,skill_name,skill_intro,shop_image,shop_name,shop_intro,user_notice,skill_notice,shop_notice,skill_equity_info_id,shop_equity_info_id,user_privacy_info_id,user_agreement_info_id,service_privacy_info_id,service_agreement_info_id,shop_privacy_info_id,shop_agreement_info_id,plus_info_id,service_ensure_info_id,shop_ensure_info_id,recharge_info_id,withdraw_info_id,exchange_info_id')
            ->find();
        if(!$row){
            $this->error('未设置相关配置');
        }
        $this->success('请求成功',$row);
    }

    /**
     * 获取文本协议
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getText()
    {
        $id = $this->request->get('id/d','');
        !$id && $this->error('参数缺失');
        $row = model('app\api\model\service\ConfigText')
            ->where(['id'=>$id])
            ->field('content')
            ->find();
        !$row && $this->error('未找到相关协议');
        $row['content'] = htmlspecialchars_decode($row['content']);
        $this->success('信息返回成功',$row);
    }

    public function templateConfig()
    {
        $templateConfig = db('service_mini_template')->where(['state'=>1])
            ->field('user_notice_template,user_order_template,order_finish_template,user_sales_template,user_complaint_template,skill_order_template,skill_sales_template,shop_order_template,shop_finish_template,shop_sales_template')
            ->find();
        $config = [];
        foreach ($templateConfig as $key=>$value)
        {
            $info = \app\api\model\service\MiniTemplate::getTemplateConfig($key);
            if($info['template_id'])
            {
                $config[$key] = $info['template_id'];
            }
        }
        $this->success('信息已处理',$config);
    }


    public function help()
    {
        $page = input('page/d',1);
        $list = \app\api\model\service\CommonQuestions::where(['state'=>1])->field('id,title,content,createtime')->order('id desc')->page($page)->limit(30)->select();
        $this->success('信息返回成功',$list);
    }


    /**
     * 用户端个人中心
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function userIndex()
    {
        $user = $this->auth->getUserinfo();
        $user['money'] = \app\api\model\service\User::where(['id'=>$user['id']])->value('money');
        $user['give_money'] = \app\api\model\service\User::where(['id'=>$user['id']])->value('give_money');
        $user['plusInfo'] = model('app\api\model\service\UserInfo')->where(['user_id'=>$user['id']])->field('is_plus,plustime,plusname')->find();
        $user['skillFollow'] = \app\api\model\service\Follow::getCount(['user_id'=>$user['id'],'state'=>1,'type'=>0]);
        $user['goodsFollow'] = \app\api\model\service\Follow::getCount(['user_id'=>$user['id'],'state'=>1,'type'=>1]);
        $user['shopFollow'] = \app\api\model\service\Follow::getCount(['user_id'=>$user['id'],'state'=>1,'type'=>2]);
        $user['orderCount'] = \app\api\model\service\Order::getCount(['user_id'=>$user['id']]);
        $user['couponCount'] = \app\api\model\service\UserCoupon::getCount(['user_id'=>$user['id'],'state'=>0]);
        $this->success('信息返回成功',$user);
    }

    /**
     * 用户余额记录
     * @return void
     */
    public function moneyList()
    {
        $uid = $this->auth->id;
        $page = input('page/d',1);
        $list = \app\api\model\service\MoneyLog::getList(['user_id'=>$uid,'page'=>$page]);
        $this->success('信息返回成功',$list);
    }

    /**
     * 用户余额记录
     * @return void
     */
    public function giveMoneyList()
    {
        $uid = $this->auth->id;
        $page = input('page/d',1);
        $list = GiveMoneyLog::getList(['user_id'=>$uid,'page'=>$page]);
        $this->success('信息返回成功',$list);
    }
    /**
     * 服务者/商户保证金记录
     * @return void
     */
    public function scoreList()
    {
        $uid = $this->auth->id;
        $page = input('page/d',1);
        $list = \app\api\model\service\ScoreLog::getList(['user_id'=>$uid,'page'=>$page]);
        $this->success('信息返回成功',$list);
    }




    /**
     * 用户修改信息
     * @return void
     */
    public function updateUserInfo()
    {
        $userInfo = $this->auth->getUserinfo();
        $type = input('type','');
        if($type == 'update')
        {
            $post = input('post.','','trim,strip_tags');
            $isUpdate = \app\api\model\service\UserInfo::where(['user_id'=>$userInfo['id']])->value('is_update');
            $user = new \app\api\model\service\User();
            $user->allowField('avatar,nickname')->save($post,['id'=>$userInfo['id']]);
            if($isUpdate == 0)
            {
                \app\api\model\service\UserInfo::where(['user_id'=>$userInfo['id']])->update(['is_update'=>1]);
            }
            $this->success('信息已更新');
        }
        $this->success('信息返回成功',$user);
    }

    /**
     * 获取服务者商户培训资料
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function trainList()
    {
        $type = input('type/d',0);
        $page = input('page/d',0);
        $list = model('app\api\model\service\Train')
            ->where(['type'=>$type,'state'=>1])
            ->field('id,title,createtime,image,videofile')
            ->page($page)
            ->limit(10)
            ->select();
        $this->success('信息返回成功',$list);
    }


    /**
     * 搜索记录列表
     * @return void
     */
    public function searchLog()
    {
        $uid = $this->auth->id;
        $type = input('type','');
        if($type == 'del')
        {
            $id = input('id/d','');
            \app\api\model\service\SearchLog::where(['id'=>$id])->delete();
            $this->success('记录已删除');
        }
        $list = SearchLog::getList(['user_id'=>$uid]);
        $this->success('信息返回成功',$list);
    }



    public function updateUserMobile()
    {
        $uid = $this->auth->id;
        $userInfo = model('app\api\model\service\UserInfo')->where(['user_id'=>$uid])->field('id,mobile')->find();
        $oldMobile = $this->request->post('oldmobile');
        $code = $this->request->post('code');
        !$oldMobile != $userInfo['mobile'] && $this->error('原有手机号输入错误');
        $mobile = $this->request->post('mobile');
        $captcha = $this->request->post('captcha');
        if (!$mobile || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $userInfo['id'])->find()) {
            $this->error(__('Mobile already exists'));
        }
        $result = Sms::check($mobile, $captcha, 'changemobile');
        if (!$result) {
            $this->error(__('Captcha is incorrect'));
        }
        $user = $this->auth->getUser();
        $verification = $user->verification;
        $verification->mobile = 1;
        $user->verification = $verification;
        $user->mobile = $mobile;
        $user->save();
        model('app\api\model\service\UserInfo')->where(['user_id'=>$uid])->update(['mobile'=>$mobile]);
        Sms::flush($mobile, 'changemobile');
    }

    public function updateUser()
    {
        $uid = $this->auth->id;
        $userInfo = \app\api\model\service\UserInfo::getUserMoney($uid);
        $type = input('type','');
        if($type == 'update')
        {
            $post = input('post.','','trim,strip_tags');
            $user = new \app\api\model\service\UserInfo();
            $user->allowField('alipay_name,alipay_account,bank_name,bank_user,bank_card')->save($post,['id'=>$userInfo['id']]);
            $this->success('信息已更新');
        }
        $this->success('信息返回成功',$userInfo);
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function extend(){
        $type = $this->request->post('type');
        /*if (!$type) {
            $this->error(__('Invalid parameters'));
        }*/
        $where['id'] = ['>',0];
        if($type){
            $where['type'] = $type;
        }
        $data = Db::name("service_extend")->where($where)->select();
        $this->success('信息返回成功',$data);
    }
}
